package com.itheima.upload.banben2;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/*
服务端
接收上传的文件 保存到服务端的本地
给客户端上传成功的结果
服务器需要可以支持不断的上传

 */
public class Server2 {
    public static void main(String[] args) throws IOException {
        //创建服务端对象 ss
        ServerSocket ss = new ServerSocket(18888);
        //接收客户端连接 ss.accept(); 获取对象 accept
        while (true) {
            Socket accept = ss.accept();
            //从连接中获取输入流 accept.getInputStream
            InputStream is = accept.getInputStream();
            //用字节缓冲流读数据
            BufferedInputStream bis = new BufferedInputStream(is);
            //创建本地字节缓冲输出流写文件
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day16\\copy.png"));
            byte[] bytes = new byte[1024];
            int len;
            //循环读写将 上传的文件保存到本地
            while ((len=bis.read(bytes))!=-1){
                bos.write(bytes,0,len);

            }
            //从连接对象中获取输出流(字节)accept.getOutputStream
            OutputStream os = accept.getOutputStream();
            //将字符输出流转换成字节输出流(OutputStreamWriter) writer
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os));
            //使用字符流写数据writer.writer()
            writer.write("上传成功!");
            //释放资源
            writer.close();
            bos.close();
            bis.close();
        }
        //ss.close();


    }


}
